package com.maybach.workflow.core.config.parser;


import com.maybach.workflow.common.api.node.BaseNode;
import com.maybach.workflow.common.api.node.ForkNode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Created by well on 15/6/11.
 */
public class ForkParser implements Parser{

    public String nodeName = NodeParser.NODE_NAME_FORK;


    @Override
    public BaseNode parse(Element element) {
        if(element == null){
            return null;
        }
        if(!nodeName.equalsIgnoreCase(element.getNodeName())){
            return null;
        }
        ForkNode forknode = new ForkNode();

        forknode.setName(element.getAttribute(NodeParser.ATTR_NAME));
        NodeList childNodes = element.getChildNodes();
        for(int i=0,len=childNodes.getLength();i<len;++i){
            Node item = childNodes.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                forknode.getForkNodeList().add(NodeParser.getParserMap().get(item.getNodeName()).parse((Element) item));
            }

        }
        check(forknode);
        return forknode;
    }

    public void check(ForkNode forknode){

    }
}
